package com.jxc.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;

import com.jxc.dao.role;
import com.jxc.pojo.Combo;
import com.opensymphony.xwork2.ActionContext;
import net.sf.json.*;
import com.jxc.entity.TbRole;

/**
 * funtion:增删改RoleAction类相关 author：zhuqiufa 创建时间2010-6-10
 */
public class RoleAction {

	
	@Resource private role roleService;
	private List list = new ArrayList();//这个是数据链表
	private int limit;  
	private int start;
	private String jsonString;//这个就是中转站了
	private int totalCount = 0;//这个是extjs用来分页记录总数
	private HttpServletRequest request; 
	private TbRole role=new TbRole();

	public int getTotalCount() {
		return totalCount;
	}
	
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	
	public String getJsonString() {
		return jsonString;
	}
	public void setJsonString(String jsonString) {
		this.jsonString = jsonString;
	}


	 //删除角色
	public void roledel()
	{
		ActionContext ctx = ActionContext.getContext();
		HttpServletResponse response = (HttpServletResponse) ctx
				.get(ServletActionContext.HTTP_RESPONSE);
		response.setHeader("Cache-Control", "no-cache");
		response.setContentType("text/json;charset=utf-8");
		
		String roleIDStr = ServletActionContext.getRequest().getParameter("roleids");
		String[] removeRoleID = roleIDStr.split(",");
		for(int i=0;i<removeRoleID.length;i++)
		{
		  int roleID=Integer.parseInt(removeRoleID[i]);
		  try
		  { 
		  this.roleService.RemoveRole(roleID);
		  }
		  catch (Exception ex) {
			  try {
					response.getWriter().write("{success:'false'}");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
			  }
		  }
		}
		 try {
			response.getWriter().write("{success:'true'}");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
		 }
	}
	
	/**
	 * 添加角色
	 */
	public void  rolesave()
	{
		ActionContext ctx = ActionContext.getContext();
		HttpServletResponse response = (HttpServletResponse) ctx
				.get(ServletActionContext.HTTP_RESPONSE);
		response.setHeader("Cache-Control", "no-cache");
		response.setContentType("text/json;charset=utf-8");
		String roleName = ServletActionContext.getRequest().getParameter("name");
		String roleCompetence = ServletActionContext.getRequest().getParameter("roleCompetence");
		boolean flag=false;
		flag=this.roleService.RoleIsExistByName(roleName.trim());
		if(!flag)
		{
			role.setName(roleName);
			role.setRoleCompetence(roleCompetence);
			if (roleService.SaveRole(role))
				try {
					response.getWriter().print("{success:'1'}");//添加成功
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			else {
				try {
					response.getWriter().print("{success:'0'}");//添加失败
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		else
		{
			try {
				response.getWriter().print("{success:'-1'}");//该数据已存在
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 修改角色
	 */
	public void  roleupdate()
	{
		ActionContext ctx = ActionContext.getContext();
		HttpServletResponse response = (HttpServletResponse) ctx
				.get(ServletActionContext.HTTP_RESPONSE);
		response.setHeader("Cache-Control", "no-cache");
		response.setContentType("text/json;charset=utf-8");
		int roleID=Integer.parseInt(ServletActionContext.getRequest().getParameter("roleId"));
		String roleName = ServletActionContext.getRequest().getParameter("name");
		String roleCompetence = ServletActionContext.getRequest().getParameter("roleCompetence");
		boolean flag=false;//是重复角色存在
		list=roleService.QueryRoleListByName(roleName);
		if(list.size()>1)
		{
			flag=true;
		}else if(list.size()==1)
		{
			TbRole oldRole=(TbRole)list.get(0);
			if(oldRole.getRoleId()!=roleID)
			{
				flag=true;
			}
		}
		
		if(!flag)
		{
			role.setName(roleName);
			role.setRoleCompetence(roleCompetence);
			role.setRoleId(roleID);
			if (roleService.UpdateRole(role))
				try {
					response.getWriter().print("{success:'1'}");//修改成功
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			else {
				try {
					response.getWriter().print("{success:'0'}");//修改失败
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		else
		{
			try {
				response.getWriter().print("{success:'-1'}");//该数据已存在
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	 //角色是存已存在
	public void roleexist()
	{
		ActionContext ctx = ActionContext.getContext();
		HttpServletResponse response = (HttpServletResponse) ctx
				.get(ServletActionContext.HTTP_RESPONSE);
		response.setHeader("Cache-Control", "no-cache");
		response.setContentType("text/json;charset=utf-8");
		String roleNameStr = ServletActionContext.getRequest().getParameter("rname");
		boolean flag=false;
		try
		{
			 flag=this.roleService.RoleIsExistByName(roleNameStr.trim());
			 if(flag)
			 {
				 try {
						response.getWriter().write("success:false");
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
					    } 
			 }
			 else
			 {
				 try {
						response.getWriter().write("success:true");
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
					    }
			 }
		}
		catch(Exception ex)
		{
			try {
				response.getWriter().write("{success:'false'}");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
		  }
		}
	}
	
	public void  roleview() {
		ActionContext ctx = ActionContext.getContext();
		HttpServletResponse response = (HttpServletResponse) ctx
				.get(ServletActionContext.HTTP_RESPONSE);
		response.setHeader("Cache-Control", "no-cache");
		response.setContentType("text/json;charset=utf-8");
		System.out.print("------------------------");
		try {  
			start = Integer.parseInt(ServletActionContext.getRequest().getParameter("start"));  
			} 
		catch (NumberFormatException e1) {  
				start = 0;  
			}  
		try {  
			 limit = Integer.parseInt(ServletActionContext.getRequest().getParameter("limit"));  
			  } 
		catch (NumberFormatException e1) {  
			    limit = 10 ;  
			}    
		if(start>0)    
			    start = start/limit;  
		list = this.roleService.QueryRoleListForPage(start, limit);
		
		List roleViewList=new ArrayList();
		for(int i=0;i<list.size();i++)
		{
			TbRole newTbRole=new TbRole();
			TbRole oldTbRole=(TbRole)list.get(i);
			newTbRole.setRoleId(oldTbRole.getRoleId());
			newTbRole.setName(oldTbRole.getName());
			newTbRole.setRoleCompetence(oldTbRole.getRoleCompetence());
			roleViewList.add(newTbRole);
		}
		this.setTotalCount(this.roleService.QueryRoleTotalRecord());
		JSONArray array = JSONArray.fromObject(roleViewList);
		this.jsonString = "{start:"+start+",limit:"+limit+",totalCount:"+this.getTotalCount()+",root:"+array.toString()+"}";
		try {
			response.getWriter().write(jsonString);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}
}
